/**
 * Created by SylarSong on 16/12/7.
 */



//************获取上一个月***************

//***********获取现在~之前6个月之间的年月信息************
/**
 * 获取现在~之前6个月之间的年月信息
 * @param timestamp  时间戳  例如: new Date().getTime();
 * @param n  需要获取的日期个数
 * @returns {Array} 返回包含年月的数组, 例如:["2016-12", "2016-11", "2016-10", "2016-9", "2016-8", "2016-7"]
 */
function getPerMonth_6(timestamp, n) {
    function getPreMonth(timestamp) {
        if (!timestamp)timestamp = new Date().getTime();
        var date = new Date(timestamp);
        var year = date.getFullYear(); //获取当前日期的年份
        var month = date.getMonth(); //获取当前日期的月份
        var day = date.getDate(); //获取当前日期的日
        var oneDay = 1000 * 3600 * 24 * 1;
        var days = new Date(year, month, 0).getDate(); //获取当前日期中月的天数
        var current_time = date.getTime();
        var after_time = current_time - (oneDay * days);
        return after_time;
    }

    if (!timestamp)timestamp = new Date().getTime();
    if (!n)n = 6;
    var res = [];
    for (var i = 0; i < n; i++) {
        res.push([new Date(timestamp).getFullYear(), new Date(timestamp).getMonth() + 1].join("-"))
        timestamp = getPreMonth(timestamp);
    }
    return res.reverse();
}
//************获取当前以前N天日期信息***************
function getPerDay(n, cycle) {
    if (!cycle)cycle = 1;
    var res = [];
    var oneDay = 1000 * 3600 * 24 * cycle;
    var now = new Date().getTime();
    for (var i = 0; i < n; i++) {
        res.push([new Date(now).getMonth() + 1, new Date(now).getDate()].join("-"))
        now -= oneDay;
    }
    return res.reverse()
}

//************制作N个假数据***************
function getRandomData(n, f, multiple) {
    if (!multiple)multiple = 1;
    var res = [];
    for (var i = 0; i < n; i++) {
        res.push(Math.floor(Math.random() * f) * multiple);
    }
    return res.reverse();
}
//************获取图标实例******************
var line_double_arpu_arppu = echarts.init($('#line-double-arpu-arppu')[0]); // 次日留存率+新增用户数
var arpu_data;
var arpu_data_by_year;

function get_arpu_distributed(params){
    if(params){
        if(!params.indexOf('&_=') >= -1){
            params += '&_=' + $.now();
        }
    }else{
        params = {
            '_': $.now()
        }
    }
    $.get('/stats_ajax/arpu_distributed', params, function(data){
        arpu_data = data;
        get_arpu_arppu(line_double_arpu_arppu, data['dates'], data['arpu'], data['arppu']);
    });
}

function get_arpu_by_year(params){
    if(params){
        if(!params.indexOf('&_=') >= -1){
            params += '&_=' + $.now();
        }
    }else{
        params = {
            '_': $.now()
        }
    }
    $.get('/stats_ajax/arpu_distributed_by_year', params, function(data){
        arpu_data_by_year = data;
        get_arpu_arppu(line_double_arpu_arppu, data['dates'], data['arpu'], data['arppu']);
    });
}

//!!!!!!!!!!!!!!!准备时间!!!!!!!!!!!!!
$(document).ready(function () {
    get_arpu_by_year();
    get_arpu_distributed();
    $('#arpu_form').submit(function(){
        get_arpu_by_year($(this).serialize());
        get_arpu_distributed($(this).serialize());
        return false;
    });

    //日按钮时间监听
    $('#btn-newPay-allPay-day').click(function () {
        event.preventDefault();
        get_arpu_arppu(
            line_double_arpu_arppu,
            arpu_data['dates'],
            arpu_data['arppu'],
            arpu_data['arpu']
        );
    });

    //月按钮时间监听
    $('#btn-newPay-allPay-month').click(function () {
        event.preventDefault();
        get_arpu_arppu(
            line_double_arpu_arppu,
            arpu_data_by_year['dates'],
            arpu_data_by_year['arppu'],
            arpu_data_by_year['arpu']

        );
    });
});




//!!!!!!!!改变尺寸触发!!!!!!!!!!!!
window.onresize = function () {
    $('#line-double-arpu-arppu').width($(".grid-12") - 50);
    line_double_arpu_arppu.resize();

};


//*新增充值用户数,累计充值用户数概况********************************************
function get_arpu_arppu(instance, date, data1, data2, cycle) {
    /***
     * @param: instance  echart 实例 例如: echart.init($(******)[0]) 获取
     * @param: date  x周时间信息;
     * @param: data1 日ARPPU 这里新增付费用户数量
     * @param: data2 日ARPPU 这里累计付费用户数量
     * @cycle: 查询周期 , 1,7,30 天
     */
    instance.setOption({});
    option = {
        title: {
            text: 'ARPU & ARPPU',
            left: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c}'
        },
        legend: {
            left: 'left',
            data: ['ARPPU', 'ARPU']
        },
        xAxis: {
            type: 'category',
            name: 'x',
            splitLine: {show: false},
            data: date

        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        yAxis: {
            axisLabel: {
                show: true,
                interval: 'auto',
                formatter: '{value} 元'
            }
        },
        series: [

            {
                name: 'ARPPU',
                type: 'line',
                data: data2
            },
            {
                name: 'ARPU',
                type: 'line',
                data: data1
            }
        ]
    };
    instance.setOption(option);
}
